﻿using CalligraphyEditor.CalligraphyData;
using CalligraphyEditor.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CalligraphyEditor
{
    /// <summary>
    /// ShowPhotoes.xaml 的交互逻辑
    /// </summary>
    public partial class ShowPhotoes : Window
    {
        VMRubbingPhotos rpvm = new VMRubbingPhotos();
        ObservableCollection<T_RubbingPhoto> rubbingPhotoes;
        BackgroundWorker bgw = new BackgroundWorker();
        public ShowPhotoes(ObservableCollection<T_RubbingPhoto> ocl)
        {
            InitializeComponent();
            InitializeBackgroundWorker();
            this.lab_u.Content = "";
            rubbingPhotoes = new ObservableCollection<T_RubbingPhoto>();
            //rubbing1 = new T_Rubbing();
            //rubbing1 = rubbing;
            foreach (var p in ocl)
            {
                rubbingPhotoes.Add(p);
            }
            this.layoutBoot.DataContext = rubbingPhotoes;
            //btn_cancel.IsEnabled = true;
            btn_start.IsEnabled = true;
            pro_UploadPhotoes1.Maximum = rubbingPhotoes.Count;
            pro_UploadPhotoes1.Minimum = 0;
        }

        private void InitializeBackgroundWorker()
        {
            bgw.WorkerReportsProgress = true;
            bgw.WorkerSupportsCancellation = true;
            bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
            bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);

        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.ToString());
            }
            else if (e.Cancelled)
            {
                btn_completed.Visibility = Visibility;
            }
            else
            {
                lab_u.Content = "上传完成";
                btn_completed.Visibility = Visibility;
            }
            //btn_cancel.IsEnabled = false;
            btn_start.IsEnabled = false;
        }

        private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ListBoxItem myListBoxItem = (ListBoxItem)(ltb_show.ItemContainerGenerator.ContainerFromItem(ltb_show.SelectedItem));
            ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
            DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
            ProgressBar progressBar = (ProgressBar)myDataTemplate.FindName("pgb_photo", myContentPresenter);
            if (e.ProgressPercentage >= rubbingPhotoes.Count)
            {

                ltb_show.SelectedIndex = e.ProgressPercentage - rubbingPhotoes.Count+1;

                pro_UploadPhotoes1.Value = e.ProgressPercentage - rubbingPhotoes.Count + 1;

                lab_UploadPercentage.Content = (e.ProgressPercentage - rubbingPhotoes.Count + 1).ToString() + "/" + rubbingPhotoes.Count.ToString();
            }
            else
            {
                progressBar.Value = e.ProgressPercentage;
            }
        }

        private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is childItem)
                    return (childItem)child;
                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);
                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker work = sender as BackgroundWorker;
            for (int i = 0; i < rubbingPhotoes.Count; i++)
            {
                //ltb_show.SelectedIndex = i;
                rpvm.UploadPhoto(rubbingPhotoes[i], i, rubbingPhotoes.Count, work, e);
            }
        }

        private void btn_start_Click(object sender, RoutedEventArgs e)
        {
            ltb_show.SelectedIndex = 0;
            pro_UploadPhotoes1.Visibility = Visibility;
            btn_start.IsEnabled = false;
            ////btn_cancel.IsEnabled = true;
            lab_u.Content = "已上传";
            bgw.RunWorkerAsync();
        }
        private void btn_completed_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
